EVS

library(readxl)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
d <- read_xlsx("EVS.xlsx")
order <- c("none", "1child", "2children", "3children", "4children", "5children", 
           "6children", "7children", "8children", "9children", "10ormore")

d %>% 
  gather("15":"100", key = "age", value = "N ") %>%
  rename("preference" = "age: respondent (constructed)",
         "N" = "N ") %>%
  mutate(preference = factor(preference, levels = order),
         preference = fct_recode(preference, 
                                 "1" = "1child", 
                                 "2" = "2children", 
                                 "3" = "3children", 
                                 "4" = "4children", 
                                 "5+" = "5children", 
                                 "5+" = "6children", 
                                 "5+" = "7children", 
                                 "5+" = "8children", 
                                 "5+" = "9children",
                                 "5+" = "10ormore")) -> long

long <- long %>% filter(age >= "18" & age <= "35" & preference != "none")

sm <- long %>% 
  dplyr::group_by(Study, preference) %>%
  dplyr::summarise(N_C = sum(N, na.rm = TRUE)) %>%
  group_by(Study) %>% 
  mutate(N_S = sum(N_C, na.rm = TRUE),
         prop = round(N_C * 100 / N_S, 0))
`summarise()` has grouped output by 'Study'. You can override using the
`.groups` argument.
stds <- c("EVS1981", "ISSP1988", "EVS1990", "ISSP1994", "GGP2003", "GGP2006")

ggplot(sm, aes(x = preference, y = prop, fill = preference)) +
  geom_col() +
  facet_wrap(~ factor(Study, levels = stds), nrow = 1) +
  guides(fill = FALSE) +
  scale_fill_viridis_d()
Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
of ggplot2 3.3.4.

sm <- sm %>%
  mutate(year = case_when(
    Study == "EVS1981" ~ 1981,
    Study == "ISSP1988" ~ 1988,
    Study == "EVS1990" ~ 1990,
    Study == "ISSP1994" ~ 1994,
    Study == "GGP2003" ~ 2003,
    Study == "GGP2006" ~ 2006
  ))

ggplot(sm, aes(x = year, y = prop, colour = preference)) +
  geom_point() +
  geom_line() +
  #guides(fill = FALSE) +
  scale_colour_viridis_d()